/*
 * COSC603 ProjectOne
 * Software Testing and Maintenance
 * Sharda Rallabhandy
 */
package projectone.stutter;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;



// The Re-engineering of sutter.c
// This program checks for stuttered words in a file and writes them with the line number to another
/**
 * @version Using JRE ${p.get('java.version')}
 * @author Sharda Rallabhandy
 * <br>
 * COSC603 ProjectOne
 * <br>
 * Software Testing and Maintenance
 * <br>
 * 
 * 
 * <h1>
 * This program takes a filename as an argument and then scans the words to report any repeated (i.e., stuttered) words in a sentence.
 * </h1>
 * <br>
 * The Class Stutter
 */
public class Stutter {

	/**
	 * Run stutter.
	 * 
	 * @param stutter
	 *            the stutter is an object of class Stutter
	 * @throws Exception
	 *             the exception which can be any kind
	 */
	
	public String runStutter(String input) throws Exception{
		// Set the initial variables
		int lineNo = 0;
		Boolean isEndOfSentence;
		String firstWord = null, nextWord, output = "";

		String[] readLine = input.split("\n");

		// while the file has data in it read line by line
		if (readLine.length > 0) {
			for(String lineRead: readLine){
			// incrementing the line counter after done with reading a line
			lineNo++;

			// Tokenize the read string by space so that we get separate
			// words
			StringTokenizer wordTokens = new StringTokenizer(lineRead);

			// while there are words in the read line get word by word
			while (wordTokens.hasMoreTokens()) {
				nextWord = wordTokens.nextToken();
				@SuppressWarnings("unchecked")
				List<Object> checkForEndOfSentence = (List<Object>) checkForEndOfSentence(nextWord);

				nextWord = (String) checkForEndOfSentence.get(0);
				isEndOfSentence = (Boolean) checkForEndOfSentence.get(1);

				// Compare the two subsequent words on a sentence or phrase
				if (firstWord != null
						&& firstWord.equalsIgnoreCase(nextWord)) {
					System.out.println("Repeated word on line " + lineNo + ": " + firstWord + " " + nextWord);
					output = output.concat("Repeated word on line " + lineNo + ": "+ firstWord + " " + nextWord + "<br>");
				}

				// If at all a punctuation mark is encountered reset the
				// counter
				if (isEndOfSentence) {
					firstWord = null;
				} else {
					//else assign second word to first word and proceed to read next word
					firstWord = nextWord;
				}
			}
		}

		}
		if(output == ""){
			output = "No stuttered words in the given text";
		}
		return output;
		}


	/**
	 * Check for end of sentence, i.e., whether the word has any punctuation mark attached to it.
	 * 
	 * @param givenWord
	 *            the 'givenWord' is the input word for this method, for which the desired check is performed
	 * @return the object which has list of two objects, first one a string without punctuation mark, 
	 * second object is a boolean value indication whether a punctuation mark was attached earlier.
	 */
	private Object checkForEndOfSentence(String givenWord) {
		// The punctuation marks are checked for while reading the words
		final String[] PUNCTUATION_MARK = { ",", ".", "!", ";", "?", ":" };
		Boolean isEndOfSentence = false;
		List<Object> checkForEndOfSentence = new ArrayList<Object>();
		for (int k = 0; k < PUNCTUATION_MARK.length; k++) {
			if (givenWord.contains(PUNCTUATION_MARK[k])) {
				givenWord = givenWord.substring(0, givenWord.indexOf(PUNCTUATION_MARK[k]));
				isEndOfSentence = true;
			}
		}
		checkForEndOfSentence.add(0, givenWord);
		checkForEndOfSentence.add(1, isEndOfSentence);

		return checkForEndOfSentence;

	}
}
